//
package com.nedjahi.premiere.application;

//
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * 
 * @author mourad
 */
public class Imc extends Activity {

	/**
	 * 
	 */
	private String message = null;
	
	//
	Button raz = null;
	Button calc = null;

	//
	EditText poids = null;
	EditText taille = null;

	//
	RadioGroup group = null;

	//
	TextView result = null;

	//
	CheckBox mega = null;

	/**
	 * 
	 */
	private  OnClickListener clicListner = new OnClickListener(){

		@Override
		public void onClick(View v) {

			switch(v.getId()){
			
			case R.id.raz :
				try{
					Imc.this.reset();
				}
				catch(NullPointerException npe){
					System.out.println(npe);
				}
				break;
			
			case R.id.calcul :
				result.setText(Imc.this.getString(R.string.default_activity_imc));
				calculate();
				break;
			}
		}		
	};

	/**
	 * 
	 */
	private OnKeyListener keyListener = new OnKeyListener(){

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			try{
				Imc.this.reset();
				return true;
			}
			catch(NullPointerException npe){
				System.out.println(npe);
				return false;
			}
		}
	};
	
	/**
	 * 
	 * @throws NullPointerException
	 */
	private void reset() throws NullPointerException {
		
		if(poids==null)
			throw new NullPointerException();
		poids.getText().clear();
		
		if(taille==null)
			throw new NullPointerException();
		
		taille.getText().clear();
	}
	
	/**
	 * 
	 */
	private void calculate(){
		
		double p = Double.valueOf(poids.getText().toString());	
		double t = Double.valueOf(taille.getText().toString());
		if(group.getCheckedRadioButtonId() == R.id.radio2){
			t = t/100;
		}
		result.setText("Votre imc est de : " + p/Math.pow(t,2));
	}

	@Override
	/**
	 * 
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imc);
		
		message = this.getString(R.string.default_activity_imc);
		poids = (EditText) this.findViewById(R.id.poids);
		taille = (EditText) this.findViewById(R.id.taille);
		result = (TextView) this.findViewById(R.id.result);
		group = (RadioGroup) this.findViewById(R.id.group);	
		mega = (CheckBox) this.findViewById(R.id.mega);
		
		raz = (Button) this.findViewById(R.id.raz);
		if(raz!=null)
			raz.setOnClickListener(clicListner);

		calc = (Button) this.findViewById(R.id.calcul);
		if(calc!=null)
			calc.setOnClickListener(clicListner);
		
		if(poids!=null){
			poids.setOnKeyListener(keyListener);
		}
		
		if(taille!=null){
			taille.setOnKeyListener(keyListener);
		}
	}

	@Override
	/**
	 * 
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
}